package com.company.biz.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.biz.user.mapper.PositionDao;
import com.company.biz.user.service.PositionService;
import com.company.common.enums.TableNamePrefixEnum;
import com.company.common.util.IdentifierGeneratorUtil;
import com.company.user.entity.PositionEntity;
import com.company.user.req.AddPositionReq;
import com.company.user.req.UpdatePositionListByPositionIdReq;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class PositionServiceImpl extends ServiceImpl<PositionDao, PositionEntity> implements PositionService {

    @Override
    public boolean addPosition(AddPositionReq req) {
        PositionEntity positionEntity = new PositionEntity();
        req.copyTo(positionEntity);
        positionEntity.setPositionId(IdentifierGeneratorUtil.getOwnId(TableNamePrefixEnum.TBPOSITION));
        Date date = new Date();
        positionEntity.setCreateTime(date);
        positionEntity.setUpdateTime(date);
        positionEntity.setPositionStatus(0);
        return save(positionEntity);
    }

    @Override
    public List<PositionEntity> getPositionListByDepartmentId(String departmentId) {
        LambdaQueryWrapper<PositionEntity> lambdaQueryWrapper = new QueryWrapper<PositionEntity>().lambda();
        lambdaQueryWrapper.eq(PositionEntity::getDepartmentId,departmentId).eq(PositionEntity::getIsDel,"n");
        return list(lambdaQueryWrapper);
    }

    @Override
    public PositionEntity getPositionListByPositionId(String positionId) {
        return this.getById(positionId);
    }

    @Override
    public boolean updatePositionListByPositionId(UpdatePositionListByPositionIdReq req) {
        PositionEntity positionEntity = new PositionEntity();
        req.copyTo(positionEntity);
        positionEntity.setUpdateTime(new Date());
        return saveOrUpdate(positionEntity, new UpdateWrapper<PositionEntity>().lambda().eq(PositionEntity::getDepartmentId, req.getPositionId()));
    }

    @Override
    public boolean deletePositionByPositionId(String positionId) {
        boolean y = update(new UpdateWrapper<PositionEntity>().lambda()
                .eq(PositionEntity::getPositionId, positionId)
                .set(PositionEntity::getIsDel, "y"));

        return y;
    }
}
